package com.zynn.service.module.es.service;

import com.zynn.common.core.utils.DateUtils;
import com.zynn.common.pojo.enums.BaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘天元
 * @description
 * @create 2018-07-01 10:47
 **/
@Service
@Slf4j
public class SyncEventService {

    @Autowired
    private RestHighLevelClient client;

    public void syncOneEvent(Long eventId,
                             Integer eventType,
                             Long userId,
                             Long groupId,
                             String accessGroupIds,
                             Integer groupTypeValue,
                             String location,
                             String title,
                             String content,
                             String publishTime,
                             Double latitude,
                             Double longitude,
                             Integer commentCount,
                             Integer likeCount,
                             Integer resourceCount) {
        Map<String, Object> jsonMap = new HashMap<>(20);
        jsonMap.put("event_id", eventId);
        jsonMap.put("event_type", eventType);
        jsonMap.put("user_id", userId);
        jsonMap.put("group_id", groupId);
        jsonMap.put("group_type_value", groupTypeValue);
        jsonMap.put("title", title);
        jsonMap.put("content", content);
        // publishTime格式校验
        Date publishTimeDate = new Date();
        if(StringUtils.isNotEmpty(publishTime)) {
            try{
                publishTimeDate = DateUtils.parse(publishTime);
            } catch (Exception e) {
                log.error(publishTime+"格式错误", e);
                publishTimeDate = new Date();
            }
        }
        publishTime = DateUtils.format(publishTimeDate);
        jsonMap.put("publish_time", publishTime);
        if (latitude != null && longitude != null) {
            jsonMap.put("latitude_longitude", latitude.toString() + "," + longitude.toString());
        }
        /// 目前的业务不需要accessGroupIds
        /*if(accessGroupIds!=null) {
            String[] split = accessGroupIds.split(",");
            Long[] accessGroupIdArray = new Long[split.length];
            for (int i=0; i<split.length; i++) {
                accessGroupIdArray[i] = Long.parseLong(split[i]);
            }
            jsonMap.put("access_group_id", accessGroupIdArray);
        }*/
        jsonMap.put("is_top", BaseEnum.IS_TOP_NO.getKey());
        jsonMap.put("location", location);
        jsonMap.put("comment_count", commentCount);
        jsonMap.put("like_count", likeCount);
        jsonMap.put("resource_count", resourceCount);
        //Map会自动转换为JSON格式的文档源
        IndexRequest indexRequest = new IndexRequest("event_index", "_doc", eventId.toString())
                .source(jsonMap);
        IndexResponse indexResponse = null;
        try {
            log.info(indexRequest.toString());
            indexResponse = client.index(indexRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info(indexResponse.toString());
    }

    public void remove(Long eventId) {
        DeleteRequest deleteRequest = new DeleteRequest("event_index", "_doc", eventId.toString());
        DeleteResponse response = null;
        try {
            response = client.delete(deleteRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info(response.toString());
    }

    public void update(Long eventId, Integer commentCount, Integer likeCount, Integer resourceCount) {
        Map<String, Object> jsonMap = new HashMap<>(10);
        if (commentCount != null) {
            jsonMap.put("comment_count", commentCount);
        }
        if (likeCount != null) {
            jsonMap.put("like_count", likeCount);
        }
        if (resourceCount != null) {
            jsonMap.put("resource_count", likeCount);
        }
        UpdateRequest updateRequest = new UpdateRequest("event_index", "_doc", eventId.toString())
                .doc(jsonMap);
        try {
            UpdateResponse updateResponse = client.update(updateRequest);
            log.info(updateResponse.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
